﻿using PPSMS_BLL;
using PPSMS_Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace PPSMS.CommonForm
{
    public  partial class ChartForm : UIForm
    {
        private InverterStatistics_BLL dataInverterStatistics_BLL = new();

        private StationStatistics_BLL dataStatistics_BLL = new();

        public static event Action<Color> ButtonColor;

        //private static ChartForm _instance=new();

        //public static ChartForm Instance
        //{
        //    get
        //    {
        //        return _instance;
        //    }
        //}

        public ChartForm()
        {
            InitializeComponent();
            DataStatisticsForm.SelectChange += (d, n) => LineChartLoad(d, n); 
        }

        private void LineChartLoad(DataTable dt, string name)
        {
            var series1 = new UILineSeries("1");
            var series2 = new UILineSeries("2");
            var series3 = new UILineSeries("3");
            UILineOption option = new UILineOption();
            option.ToolTip.Visible = true;
            option.Title = new UITitle();
            option.Title.Text = "历史记录曲线图";
            
            option.XAxisType = UIAxisType.DateTime;

            if (dt.Rows.Count == 0)
            {
                option.Title.Text = "该事件段暂无数据";
                return;
            }

            if (name == "STATION_STATISTICS")
            {
                option.Title.SubText = "电站数据统计图";
                series1 = option.AddSeries(new UILineSeries("电站累计发电量", Color.Lime));
                series2 = option.AddSeries(new UILineSeries("电站累计收益", Color.Red));
                series3 = option.AddSeries(new UILineSeries("累计二氧化碳减排", Color.Blue));

            }
            else
            {
                option.Title.SubText = "逆变器统计数据图";
                series1 = option.AddSeries(new UILineSeries("日发电量/kW·h ", Color.Green));
                series2 = option.AddSeries(new UILineSeries("累计发电量/kW·h", Color.Black));
                series3 = option.AddSeries(new UILineSeries("累计二氧化碳减排/t", Color.Orange));
            }
            foreach (DataRow dr in dt.Rows)
            {
                ChartDataLoad(series1, series2, series3, dr);
                option.XAxis.SetRange(Convert.ToDateTime(dt.Rows[0]["DATE"].ToString()), Convert.ToDateTime(dt.Rows[dt.Rows.Count - 1]["DATE"].ToString()).AddHours(1));
            }
            LineSet(series1);
            LineSet(series2);
            LineSet(series3);
            option.Legend=new UILegend();
            option.Legend.Orient = UIOrient.Vertical;
            option.Legend.Top = UITopAlignment.Top;
            option.Legend.Left = UILeftAlignment.Left;
            option.Legend.Style=UILegendStyle.Line;
            option.Legend.AddData(series1.Name,series1.Color);
            option.Legend.AddData(series2.Name,series2.Color);
            option.Legend.AddData(series3.Name,series3.Color);
            option.XAxis.Name = "日期";
            option.YAxis.Name = "数值";

            //X轴坐标轴显示格式化
            // option.XAxis.AxisLabel.DateTimeFormat = "yyyy-MM-dd ";

            //Y轴坐标轴显示小数位数
            option.YAxis.AxisLabel.DecimalPlaces = 1;
            uiLineChart1.SetOption(option);
        }

        private static void ChartDataLoad(UILineSeries series1, UILineSeries series2, UILineSeries series3, DataRow dr)
        {
            series1.Add(Convert.ToDateTime(dr["DATE"].ToString()), Convert.ToDouble(dr[dr.Table.Columns.Count - 3].ToString()));
            series2.Add(Convert.ToDateTime(dr["DATE"].ToString()), Convert.
                ToDouble(dr[dr.Table.Columns.Count - 2].ToString()));
            series3.Add(Convert.ToDateTime(dr["DATE"].ToString()), Convert.ToDouble(dr[dr.Table.Columns.Count - 1].ToString()));
        }

        private static void LineSet(UILineSeries series)
        {
            series.Symbol = UILinePointSymbol.Diamond;
            series.SymbolSize = 6;
            series.SymbolLineWidth = 2;

            //数据点显示小数位数
            series.YAxisDecimalPlaces = 2;
            series.Smooth = false;

            series.XAxisDateTimeFormat = "yyyy-MM-dd";
        }

        private void ChartForm_Load(object sender, EventArgs e)
        {
            FormClosing += (s, e) => { ButtonColor?.Invoke(UIColor.LayuiBlue); };
        }  
    }
}
